#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_NoticeOfMaintenanceOrBLPartitionResultSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'NoticeOfMaintenanceOrBLPartitionResult'
    
    #Read from inn by XML order

    #NoticeOfMaintenanceOrBLPartitionResult/Document
    DocumentTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':None})
    DocumentFunctionTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'FunctionTypeCode':None})
    DocumentIssueDate = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDate':None})
    
    #NoticeOfMaintenanceOrBLPartitionResult/UCR
    UCRIdentificationID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'UCR',ns2+'IdentificationID':None})
    
    #NoticeOfMaintenanceOrBLPartitionResult/CustomsOffice
    CustomsOfficeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'OfficeCode':None})
    CustomsOfficeCodeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'CodeID':None})
    CustomsOfficeOfficerName = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice'},{'BOTSID':ns2+'CustomsOfficer',ns2+'Name':None})
    
    #NoticeOfMaintenanceOrBLPartitionResult/PreviousCustomsDocument
    PreviousCustomsDocumentReferenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'referenceID':None})
    
    out.ta_info['botskey'] = 'CUSCAR-5HF-' + UCRIdentificationID.strip()
    
    #Write to out by EDIFACT order
    
    out.put({'BOTSID':'UNH','0062':out.ta_info['reference']})
    out.put({'BOTSID':'UNH','S009.0065':'CUSCAR'})
    out.put({'BOTSID':'UNH','S009.0052':'S'})
    out.put({'BOTSID':'UNH','S009.0054':'93A'})
    out.put({'BOTSID':'UNH','S009.0051':'KE'})
            
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':DocumentTypeCode})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':UCRIdentificationID})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1225':DocumentFunctionTypeCode})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'150','C507.2380':utils.edifactYYYYMMDD(DocumentIssueDate),'C507.2379':'102'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','3227':'41','C517.3225':CustomsOfficeCodeID,'C517.1131':'113','C517.3055':'KCS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','C519.3223':CustomsOfficeCode,'C519.1131':'5AB','C519.3055':'KCS'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF','C506.1153':'ABP','C506.1154':PreviousCustomsDocumentReferenceID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','3035':'AM'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','C058.3124#1':CustomsOfficeOfficerName})
    

    # Loop 1 ~ 99
    #NoticeOfMaintenanceOrBLPartitionResult/PreviousCustomsDocument
    for gid in inn.getloop({'BOTSID':rootelement},{'BOTSID':ns1+'MaintenanceOrBLPartitionResult'}):
        MRCargoInformationCode = gid.get({'BOTSID':ns1+'MaintenanceOrBLPartitionResult',ns2+'CargoInformationCode':None})
        MRWeightMeasure = gid.get({'BOTSID':ns1+'MaintenanceOrBLPartitionResult',ns2+'WeightMeasure':None}) 
        MRWeightMeasure_measureUnitCode = gid.get({'BOTSID':ns1+'MaintenanceOrBLPartitionResult',ns2+'WeightMeasure__measureUnitCode':None})
        MRPartitionOrCancellationHSNID = gid.get({'BOTSID':ns1+'MaintenanceOrBLPartitionResult',ns2+'PartitionOrCancellationHSNID':None})
        MRNewCarryInReportNumberID = gid.get({'BOTSID':ns1+'MaintenanceOrBLPartitionResult',ns2+'NewCarryInReportNumberID':None})
        MRGoodsPackagingQuantity = gid.get({'BOTSID':ns1+'MaintenanceOrBLPartitionResult'},{'BOTSID':ns2+'GoodsPackaging',ns2+'Quantity':None})
        MRGoodsPackagingTypeCode = gid.get({'BOTSID':ns1+'MaintenanceOrBLPartitionResult'},{'BOTSID':ns2+'GoodsPackaging',ns2+'TypeCode':None})
        
        lou = out.putloop({'BOTSID':'UNH'},{'BOTSID':'GID'})
        lou.put({'BOTSID':'GID','1496':MRCargoInformationCode})
        
        lou.put({'BOTSID':'GID'},{'BOTSID':'PAC','7224':MRGoodsPackagingQuantity})
        lou.put({'BOTSID':'GID'},{'BOTSID':'PAC','7224':MRGoodsPackagingQuantity,'C202.7065':MRGoodsPackagingTypeCode})

        lou.put({'BOTSID':'GID'},{'BOTSID':'MEA','6311':'WT','C174.6411':MRWeightMeasure_measureUnitCode,'C174.6314':MRWeightMeasure})
        lou.put({'BOTSID':'GID'},{'BOTSID':'RFF','C506.1153':'BH','C506.1154':MRPartitionOrCancellationHSNID})
        lou.put({'BOTSID':'GID'},{'BOTSID':'RFF','C506.1153':'ABT','C506.1154':MRNewCarryInReportNumberID})
    # End Loop
        
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0074':out.getcount()+1})
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0062':out.ta_info['reference']}) 
    
